import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.AbstractCollection;
import java.util.logging.*;
/**
 * This is the income statement for the hospital.  
 * An income statement simply returns the total calculations based on a specified month.
 * and is done for a specific month/year.  It must also display all patients seen for that
 * specific time period.
 * @author Matt Lee
 *
 */

//public class IncomeStatement
public class IncomeStatement implements Serializable{
	private ArrayList<Patient> patientsSeen;

	private double billTotal;
	
	private int month;
	
	private int day;
	
	private int year;
	
	private static Logger incomeStatementLog = 
		Logger.getLogger(IncomeStatement.class.getName());
	
	static{
		ConsoleHandler console = new ConsoleHandler();
		console.setLevel(Level.ALL);
		console.setFormatter(new ThunderDragonHtmlFormatter());
		incomeStatementLog.addHandler(console);
		incomeStatementLog.setLevel(Level.ALL);
		
		try {
			FileHandler fh = new FileHandler("IncomeStatementLog.txt");
			fh.setFormatter(new ThunderDragonHtmlFormatter());
			incomeStatementLog.addHandler(fh);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			incomeStatementLog.severe("Failure: Security exception thrown" +
					" when trying to create a new handler" +
					" in IncomeStatement.java.");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			incomeStatementLog.severe("Failure: I/O exception thrown" +
					" when trying to create a new handler" +
					" in IncomeStatement.java.");
			e.printStackTrace();
		}
		
	}

	public IncomeStatement(){
		incomeStatementLog.entering(this.getClass().getName(), 
				"IncomeStatement Constructor 1");
		patientsSeen = new ArrayList<Patient>();
		billTotal = 0;
		month = 0;
		year = 0;
		incomeStatementLog.exiting(this.getClass().getName(), 
				"IncomeStatement Constructor 1");
	}
	
	public IncomeStatement(int myMonth, int myYear){
		patientsSeen = new ArrayList<Patient>();
		billTotal = 0;
		month = myMonth;
		year = myYear;
	}
	
	//Might need to edit this constructor.  The problem with this is that we're looking
	//at ALL patients, not those that visited in the specific month/year
	public IncomeStatement(ArrayList<Patient> patientList,
			double calculatedTotal, int myMonth, int myYear){
		incomeStatementLog.entering(this.getClass().getName(), 
				"IncomeStatement Constructor 2", new Object[]
			{patientList, calculatedTotal, myMonth, myYear});
		
		patientsSeen = patientList;
		billTotal = calculatedTotal;
		month = myMonth;
		year = myYear;
		incomeStatementLog.exiting(this.getClass().getName(), 
				"IncomeStatement Constructor 2");
	}
	
	

	/**
	 * Calculates the total income statement.  
	 * It is kept private because it should be displayed
	 * specifically under viewStatement.
	 * @return
	 */
	private double calculateStatement(){
		
		//First grab the medical records from the medical record database.
		//Go through all the treatment records.
		//If the month and year match, get the invoice.
		//Inefficient?  Oh yes.
		
		double totalStatement =0;
		for(MedicalRecord mRecord: MedicalRecordDatabase.recordsList){
			ArrayList<TreatmentRecord> tRecord =
				(ArrayList<TreatmentRecord>) mRecord.findByMonth(month);
			for(TreatmentRecord t: tRecord){
				PatientInvoice invoice = new PatientInvoice(t);
				totalStatement = totalStatement+invoice.getPatientBill();
			}
		}
		
		return totalStatement;
	}
	
	private ArrayList<Patient> seePatientsSeen(){
		for(MedicalRecord mRecord: MedicalRecordDatabase.recordsList){
			ArrayList<TreatmentRecord> tRecord =
				(ArrayList<TreatmentRecord>) mRecord.findByMonth(month);
			if(tRecord.size()>0){
				patientsSeen.add(mRecord.patient);
			}
	}
		return patientsSeen;
	}
	
	/**
	 * This will visually show the statement for the income.
	 */
	public void viewStatement(){
		//Think of a design for the income statement.
		//Insert code here.
		incomeStatementLog.finest("Exiting viewStatement() in class IncomeStatement.");
	}

	/**
	 * This method returns the next month.
	 * @return the integer representing the next month.
	 */
	public int newMonth(){
		incomeStatementLog.finest("Got the new month.");
		if(month==12){
			return 1;
		}
		return month + 1;
	}

	@Override
	public String toString() {
		return "IncomeStatement [patientsSeen=" + patientsSeen + ", billTotal="
				+ billTotal + ", month=" + month + ", year=" + year + "]";
	}
	
}